<template>
	<view :style="[pageStyle, AppSkinVar]">
		<layout :loading="loading" :componentsList="componentsList"></layout>
		<!-- 弹窗广告组件 -->
		<i-ad-dialog :option="advertisingSetting" v-if="advertisingSetting.openAd" />
	</view>
</template>

<script lang="ts">
	import skinMixIn from '@/common/mixin/skinMixIn';
	import layoutPageTitleMixIn from '@/common/mixin/layoutPageTitleMixIn';
	export default {
		mixins: [skinMixIn, layoutPageTitleMixIn]
	};
</script>
<script lang="ts" setup>
	import { onLoad, onShow,onHide, onReachBottom } from '@dcloudio/uni-app';
	import { useLayoutPage, useCurrentInstance,useLogin } from '@/common/hooks';
	// #ifdef PRIVATE || SUBSCRIPTION
	import { wxAppMessageShareData, wxTimelineShareData, getShareLink } from '@/common/wxInit';
	// #endif
	import type { MpShare } from '@/common/utils';
	import {postSupperAppLogin} from '@/common/request/api'
	import { layoutPageContextKey } from '@/token';
	import { ref, reactive, provide, onMounted } from 'vue';
	const contextId = ref('preview');
		provide(
			layoutPageContextKey,
			reactive({
				contextId
			})
		);
	const ctx = useCurrentInstance();
	const { login } = useLogin();
	const { loading, componentsList, pageStyle, advertisingSetting, init, mpShare } = useLayoutPage(ctx);

	const prop = defineProps<{
		id : string;
	}>();
	const appLogin = () => {
		return new Promise((resolve, reject) => {
			// #ifdef APP
			uni.sendNativeEvent(
				'autoLogin',
				{
					msg: 'sendAutoLogin'
				},
				(ret) => {
					let params = {
						code: ret,
						salt: uni.getStorageSync('userSalt')
					};
					postSupperAppLogin(params)
						.then(async (res) => {
							uni.removeStorageSync('userSalt');
							await login(res.data.access_token);
							resolve()

						})
						.catch((err) => {
							reject(err);
						})
				}
			);
			return;
			// #endif
			resolve()
		})
	}
	onLoad(async () => {
		if(!uni.getStorageSync('token')){
			await appLogin()
		}
		init(prop.id);
	});
	onHide(() => {
		contextId.value = '';
	});
	onShow(() => {
		contextId.value = 'preview';
		let _mpShare = mpShare;
		// #ifdef PRIVATE || SUBSCRIPTION
		if (Object.keys(_mpShare).length === 0) {
			_mpShare = {
				title: '', // 分享标题
				desc: '', // 分享描述
				link: getShareLink(), // 分享链接
				imgUrl: '' // 分享图标
			};
		}
		wxAppMessageShareData(_mpShare as MpShare);
		wxTimelineShareData(_mpShare as MpShare);
		// #endif
		uni.$endless.mpShare = _mpShare;
	});
	onReachBottom(() => {
		uni.$emit('onLayoutPageReachBottom');
	});
</script>